
#include <BFont>
#include <BScrollArea>

#include "member_BListWidget.h"
#include "member_BListItem.h"

using namespace BWE;

member_BListWidget::member_BListWidget(BListWidget* listWidget)
{
	boss = listWidget;
	orientation = Orientation_Vertical;
	selectMode = Select_Multiple | Select_Continuous;
	showBegin = -1;
	showEnd = -1;
	itemStyle = 0;
	itemAlign = Align_LeftCenter;
	itemPolicy = Policy_Growing;
	itemSpacing = 1;

	dirty = false;
	dirtyVisual = false;
	pressed = 0;
	hoveredItem = 0;
}
member_BListWidget::~member_BListWidget()
{

}

void member_BListWidget::freshArea()
{
	const BStyle* style = boss->realStyle();
	const BFont* font = style->font();

	for (int i = 0; i < items.size(); i++)
	{
		BListItem* item = items[i];
		item_member(item)->index = i;
		if (item_member(item)->dirtyStyle)
		{
			item_member(item)->dispenseStyles(style);
			item_member(item)->applyStyle(style);
			item_member(item)->dirtyStyle = false;
			item_member(item)->dirty = true;
		}
		if (item_member(item)->dirty)
		{
			item_member(item)->freshSize(font, 0);
			item_member(item)->dirty = false;
		}
	}

	int width = 0;
	int height = 0;
	if (orientation == Orientation_Vertical)
	{
		for (int i = 0; i < items.size(); i++)
		{
			BListItem* item = items[i];
			item_member(item)->index = i;
			if (!item_member(item)->visible)
				continue;
			BSize& size = item_member(item)->hintSize;
			if (size.height() < itemSize.height())
				size.height() = itemSize.height();
			if (size.width() < itemSize.width())
				size.width() = itemSize.width();
			item_member(item)->setPos(0, height);
			item_member(item)->setSize(size);
			width = bMax(size.width(), width);
			height += itemSpacing + size.height();
		}
	}
	if (orientation == Orientation_Horizontal)
	{
		for (int i = 0; i < items.size(); i++)
		{
			BListItem* item = items[i];
			item_member(item)->index = i;
			if (!item_member(item)->visible)
				continue;
			BSize& size = item_member(item)->hintSize;
			if (size.height() < itemSize.height())
				size.height() = itemSize.height();
			if (size.width() < itemSize.width())
				size.width() = itemSize.width();
			item_member(item)->setPos(width, 0);
			item_member(item)->setSize(size);
			height = bMax(size.height(), height);
			width += itemSpacing + size.width();
		}
	}
	BRect area = boss->area();
	area.setSize(width, height);
	boss->setArea(area);
}
void member_BListWidget::freshVisualItems()
{
	BRect area = boss->area();
	int areaWidth = area.width();
	BPoint offset = area.pos();
	BRect crect = boss->centerRect() - offset;
	int width = crect.width();

	showBegin = -1;
	showEnd = -1;

	for (int i = 0; i < items.size(); i++)
	{
		BListItem* item = items[i];
		if (!item_member(item)->visible)
			continue;
		BRect rect = item_member(item)->rect();
		if (rect.bottom() < crect.top())
			continue;
		if (rect.top() > crect.bottom())
			break;
		BSize size = item_member(item)->size;
		if (itemPolicy & Policy_Expanding && size.width() < areaWidth)
			size.width() = areaWidth;
		item_member(item)->setSize(size);
		if (showBegin < 0)
		{
			showBegin = i;
			showEnd = i;
		}
		else
		{
			showEnd = i;
		}
	}
	for (int i = 0; i < items.size(); i++)
	{
		BListItem* item = items[i];
		if (BWidget* widget = item_member(item)->widget)
		{
			BPoint absPos = item_member(item)->pos + offset;
			widget->setRect(item_member(item)->ia_iconRect + absPos);
		}
	}
}
